<?php

defined('BY_JYA') or exit('error');
$cashierid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	$order['oldprice']=$order['price'];
	//定义订单状态名称
	if($order['status']==0 && $order['paytype']!=3){
		$order['statustitle']='订单待支付';
		$order['statusdesc']='超时未支付，订单将自动取消';
	}elseif($order['status']==0 && $order['paytype']==3){
		$order['statustitle']='货到付款';
		$order['statusdesc']='请您收到商品后付款';
	}elseif($order['status']==1 && !$order['refundstate']){
		$order['statustitle']='买家已付款';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==2 && !$order['refundstate']){
		$order['statustitle']='商家已发货';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==3 && !$order['refundstate']){
		$order['statustitle']='订单已完成';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==4 && !$order['refundstate']){
		$order['statustitle']='订单已关闭';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==5){
		$order['statustitle']='申请退款中';
		$order['statusdesc']='请您留意退款进度';
	}elseif($order['status']==5 && $order['oldstatus']>1){
		$order['statustitle']='申请售后中';
		$order['statusdesc']='请您留意售后进度';
	}elseif($order['status']==6){
		$order['statustitle']='申请售后成功';
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}else{
		$order['statustitle']='订单已关闭';
		$order['statusdesc']='欢迎下次光临！';
	}
	//包含核销商品
	if($order['status']==2 && $order['isverify']==1){
		$order['statustitle']='部分商品已发货';
		$order['statusdesc']='除核销商品，其他商品已发货';
	}elseif($order['status']==2 && $order['isverify']==2){
		$order['statustitle']='等待核销';
		$order['statusdesc']='属核销商品，请核销！';
	}elseif($order['status']==3 && $order['isverify']==2){
		$order['statustitle']='已核销';
		$order['statusdesc']='属核销商品，已核销完成！';
	}
	//用户地址
	if($order['addressid']){
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$order['express']=pdo_get('jy_weishop_order_express',array('orderid'=>$order['id'],'opensend_type'=>0));
	$order['express']['express_name']=$this->express_types[$order['express']['express_id']]['name'];
	//时间格式化
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	$order['paytime']=date('Y-m-d H:i:s',$order['paytime']);
	$order['sendtime']=$order['express']['createtime']?date('Y-m-d H:i:s',$order['express']['createtime']):'';
	$order['finishtime']=date('Y-m-d H:i:s',$order['finishtime']);
	if ($order['userdeleted'] == 2) {
			exi('订单已经被删除!','error');
	}
	$order['total_price']=0;
	$goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
	if($goods){
		foreach($goods as $k=>&$gv){
			$order['total_price'] +=$gv['total_price'];
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','unit','weight','type','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			$gv['title']=$goodsinfo['title'];
			$gv['image']=$goodsinfo['image'];
			$gv['totalprice']=$gv['total']*$gv['price'];
			$gv['merchid']=$goodsinfo['merchid'];	
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$goodsinfo['buytypetitle']=$this->buytypes()[$order['buytype']];
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$return['ismerch']=1;
			}
			$gv['goodsinfo']=$goodsinfo;
		}
	}
	$return['goods']=$goods;
	$return['nowgoodskey']=0;
	$return['nowgoods']=$goods[0];
	$return['order']=$order;
	$return['member']=$member;
	$return['address']=$address;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	 if($postdata['order']['addprice']){
		pdo_update('jy_weishop_order',array('oldprice'=>$postdata['order']['oldprice'],'changeprice'=>$postdata['order']['addprice'],'addprice'=>$postdata['order']['addprice']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['order']['id']));
		//print_r($postdata);exit;
		foreach($postdata['goods'] as $k=>&$gv){
			if($gv['ischange']){
				pdo_update('jy_weishop_order_goods',array('total'=>$gv['total'],'total_price'=>$gv['total_price']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['id']));
			}
		}
		exi('更新订单商品信息成功','','/manage.lineorder/');
	}else{
		exi('多退少补价为0，无需更新商品信息','error');
	}		
}
